<?php

$V5_TIME = local_gettime();
$V5_NOW = date('Y-m-d H:i:s', $V5_TIME);
$V5_DATE = date('Y-m-d', $V5_TIME);

$router = $request->get('route', 'common/home');
$path = '';
$parts = explode('/', str_replace(array('../', '..\\', '..'), '', $router));//转化为数组
foreach ($parts as $part) { 
	$path .= $part;	
	if (is_dir(DIR_APPLICATION . 'controller/' . $path)) {
		$path .= '/';
		array_shift($parts);
		continue;
	}	
	if (is_file(DIR_APPLICATION . 'controller/' . $path . '.php')) {
		array_shift($parts);
		break;
	}
}
$method = array_shift($parts);
if (!$method) $method = 'index';
unset($parts);
define('ROUTE', $path);
define('METHOD', $method);

$system_data = cache_read('system-'. C('config_language_id') .'.php');
foreach ($system_data as $key => $val) {
	$config->set($key, $val);
}
unset($system_data);

$CATEGORIES = cache_read("category-0-". C('config_language_id') .".php");

/**
 *  Admin base controller
 * 
 */
class AdminController extends Controller {
	
	protected $vo = null;
	
	/**
	 * construct
	 * @param object $registry
	 * @param string $route
	 * @param string $method
	 */
	public function __construct($registry, $route = '', $method = '') {
		parent::__construct($registry, $route, $method);
				
		//autoload language
		$this->language->load($route);
		
		if ($route != ROUTE) return;

		
		//has permission?
		if ($this->request->isPost() 
				&& in_array($method, array('insert', 'update', 'delete'))) {
			if (!$this->user->hasPermission('modify', $route)) {
				$this->redirect(UA('error/permission'));
			}
		}
		
		//default layout
		$this->children = array(
				'common/header',
				'common/footer'
		);
		
		//flash data
		$this->data['success'] = $this->session->flashdata('success');
		
		//autoload helpers
		$this->load->helper('query_filter');
		$this->load->helper('vo');

		//view object
		$this->vo = new Vo();
		$this->data['vo'] = $this->vo;
		
	}
	
	/**
	 * has modify permission?
	 */
	protected  function hasMP() {
		return $this->user->hasPermission('modify', $this->route);
	}
	
	/**
	 * get pagination object
	 */
	protected function pagination($total = 0, $url = null, $limit = null, $page = null) {
		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $page ? $page : $GLOBALS['_PAGE'];
		$pagination->limit = $limit ? $limit : C('config_admin_limit');
		$pagination->text = $this->_['text_pagination'];
		$pagination->url = $url ? $url : UA($this->route, 'page={page}');
		return $pagination;
	}

	
	/**
	 * bread crumbs
	 * @return string
	 */
	protected function breadCrumbs() {
		$crumbs = '<a href="'. UA('common/home') .'">'. $this->_['text_home'] .'</a>';
		$crumbs .= ' :: <a href="'. UA(ROUTE) .'">'. $this->_['heading_title'] .'</a>';
		return $crumbs;
	}
}

/******************************************
 * 
 * functions
 *
 ******************************************/

//admin url
function UA($route, $args = '', $connection = 'SSL') {
	global $url, $session;
	return $url->link($route, $args . "&token=" . $session->data['token'], $connection);
}

//catalog url
function U($route, $args = '', $connection = 'NONSSL') {
	global $catalog_url;
	return $catalog_url->link($route, $args, $connection);
}

/**
 * sort url
 * $GLOBALS['_SORT'], $GLOBALS['_ORDER'] are set in Query_filter 
 * @param $sort
 * @param $default
 * @return string
 */
function sort_url($sort, $default = false) {
	$current = $GLOBALS['_SORT'];
	if ($sort == $current || (!$current && $default)) {
		$order = $GLOBALS['_ORDER'];
		$text = '" class="' . strtolower($order) . '"';
		$order = ($order == 'ASC') ? 'DESC' : 'ASC';
		return 'href="' . UA(ROUTE, "sort=$sort&order=$order") . $text;
	}
	else {
		return 'href="' . UA(ROUTE, "sort=$sort&order=ASC") . '"';
	}
}

//seo url
function seo_url($str) {
	$url = preg_replace("/[^a-zA-Z0-9 ]/", " ",$str);		
	$url = preg_replace("/\s+/","-",$url);	
	$url = trim($url, "-");
	return strtolower($url);
}

function get_award_stars($grade) {
	return str_repeat('<img src="view/image/award_star_gold.png" />', $grade);
}

function user_log($username, $info) {
	global $db;
	$data = array(
			'username' => $username,
			'info' => $info,
			'ip' => $_SERVER['REMOTE_ADDR'],
			'date_added' => $GLOBALS['V5_NOW']
	);
	$db->insert('user_log', $data);
}